#ifndef __GAF_MEMHAND_H__
#define __GAF_MEMHAND_H__

#include "GAF_Define.h"

#ifdef GAF_SAFE_MEMORY

namespace GAF
{
	/**
 	 *  @addtogroup Memory
	 *  @{
	 */

	/// 内存指针基类
	class BaseHand
	{
	public:
		/// 获得内存指针
		virtual void*		GetPointer() = 0;
		/// 删除内存
		virtual void		ReleasePointer() = 0;
		/// 获得内存大小
		virtual size_t		GetSize_a() = 0;

//	protected:
		/// 内存地址
		void*			m_pAddress;
		UINT			m_nCounst;
	};

	/// 内存句柄模板类
	template<class T>
	class MemHand : public BaseHand
	{
	public:
		MemHand(T* pAddress);
		MemHand(T* pAddress, UINT nCounst);
		~MemHand(){};

	public:
		void*		GetPointer();
		void		ReleasePointer();
		size_t		GetSize_a();
	};

	template<class T>
	MemHand<T>::MemHand(T* pAddress)
	{
		m_pAddress = pAddress;
		m_nCounst = 1;
	}

	template<class T>
	MemHand<T>::MemHand(T* pAddress, UINT nCounst)
	{
		if(pAddress == NULL)
			int sss = 0;

		m_pAddress = pAddress;
		m_nCounst = nCounst;
	}

	template<class T>
	void* MemHand<T>::GetPointer()
	{
		return (T*)m_pAddress;
	}

	template<class T>
	void MemHand<T>::ReleasePointer()
	{
		if(m_nCounst > 0)
		{
			for(UINT _i = 0; _i < m_nCounst; _i++)
			{
				((T*)m_pAddress + _i)->~T();
			}
		}
// 		else
// 			GAF_ERRMARK();
	}

	template<class T>
	size_t MemHand<T>::GetSize_a()
	{
		return sizeof(T) * m_nCounst;
	}
	/** @} */

}
#endif /*GAF_SAFE_MEMORY*/
#endif
